package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Random;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SysAsset;
import com.ruoyi.system.domain.vo.SysAssetBorrowApplyVo;
import com.ruoyi.system.mapper.SysAssetMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysAssetBorrowApplyMapper;
import com.ruoyi.system.domain.SysAssetBorrowApply;
import com.ruoyi.system.service.ISysAssetBorrowApplyService;

/**
 * 资产借出申请Service业务层处理
 * 
 * @author sxy
 * @date 2025-03-26
 */
@Service
public class SysAssetBorrowApplyServiceImpl implements ISysAssetBorrowApplyService 
{
    @Autowired
    private SysAssetBorrowApplyMapper sysAssetBorrowApplyMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysAssetMapper sysAssetMapper;

    /**
     * 查询资产借出申请
     * 
     * @param applyId 资产借出申请主键
     * @return 资产借出申请
     */
    @Override
    public SysAssetBorrowApply selectSysAssetBorrowApplyByApplyId(Long applyId)
    {
        return sysAssetBorrowApplyMapper.selectSysAssetBorrowApplyByApplyId(applyId);
    }

    /**
     * 查询资产借出申请列表
     * 
     * @param sysAssetBorrowApply 资产借出申请
     * @return 资产借出申请
     */
    @Override
    public List<SysAssetBorrowApplyVo> selectSysAssetBorrowApplyList(SysAssetBorrowApply sysAssetBorrowApply)
    {
        return sysAssetBorrowApplyMapper.selectSysAssetBorrowApplyList(sysAssetBorrowApply);
    }

    /**
     * 新增资产借出申请
     * 
     * @param sysAssetBorrowApply 资产借出申请
     * @return 结果
     */
    @Override
    public int insertSysAssetBorrowApply(SysAssetBorrowApply sysAssetBorrowApply)
    {
        sysAssetBorrowApply.setCreateTime(DateUtils.getNowDate());
        //查询资产管理员列表
        List<Long> assetAdmin = sysUserMapper.selectRoleUser("asset_admin");
        //随机指定审核人
        sysAssetBorrowApply.setReviewerId(assetAdmin.get(new Random().nextInt(assetAdmin.size())));
        //状态默认待审核
        sysAssetBorrowApply.setReviewStatus("待审核");
        return sysAssetBorrowApplyMapper.insertSysAssetBorrowApply(sysAssetBorrowApply);
    }

    /**
     * 修改资产借出申请
     * 
     * @param sysAssetBorrowApply 资产借出申请
     * @return 结果
     */
    @Override
    public int updateSysAssetBorrowApply(SysAssetBorrowApply sysAssetBorrowApply)
    {
        int i = sysAssetBorrowApplyMapper.updateSysAssetBorrowApply(sysAssetBorrowApply);
        if("通过".equals(sysAssetBorrowApply.getReviewStatus())){//借用申请通过
            //修改资产状态为借出
            SysAsset sysAsset = new SysAsset();
            sysAsset.setAssetId(sysAssetBorrowApplyMapper.selectSysAssetBorrowApplyByApplyId(sysAssetBorrowApply.getApplyId()).getAssetId());
            sysAsset.setStatus("借出");
            sysAssetMapper.updateSysAsset(sysAsset);
        }
        return i;
    }

    /**
     * 批量删除资产借出申请
     * 
     * @param applyIds 需要删除的资产借出申请主键
     * @return 结果
     */
    @Override
    public int deleteSysAssetBorrowApplyByApplyIds(Long[] applyIds)
    {
        return sysAssetBorrowApplyMapper.deleteSysAssetBorrowApplyByApplyIds(applyIds);
    }

    /**
     * 删除资产借出申请信息
     * 
     * @param applyId 资产借出申请主键
     * @return 结果
     */
    @Override
    public int deleteSysAssetBorrowApplyByApplyId(Long applyId)
    {
        return sysAssetBorrowApplyMapper.deleteSysAssetBorrowApplyByApplyId(applyId);
    }
}
